package javaSE.Thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class createByCallable {
    public static void main(String[] args) {
        //1.先创建callable实现类对象
        thread03 thread03 = new thread03();
        //2.将callable实现类对象通过FutureTask类包装，特别是FutureTask对象的get方法能获取线程的返回值
        FutureTask<String> futureTask = new FutureTask<String>(thread03);
        //将FutureTask类对象作为Thread类的参数创建线程。
        Thread thread = new Thread(futureTask);
        thread.start();

        //注意，这种方式创建线程是可以有返回值，而且是可以进行异常处理的。
        try {
            String returnValue = futureTask.get();
            System.out.println("通过FutureTask类对象的get方法获取线程返回值" + returnValue);
        } catch (InterruptedException e) {
            System.out.println(e.getMessage());
        } catch (ExecutionException e) {
            System.out.println(e.getMessage());
        }

    }
}

class thread03 implements Callable {

    @Override
    public String call() throws Exception {
        System.out.println("通过实现callable来创建线程");
        String str = new String("这是call方法的返回值");
        return str;
    }
}
